package com.zqxx.examine.portal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.KnowledgePoint;
import com.zqxx.examine.model.question.Question;
import com.zqxx.examine.model.question.QuestionQueryResult;
import com.zqxx.examine.model.question.QuestionStatistic;
import com.zqxx.examine.model.question.QuestionType;
import com.zqxx.examine.portal.persistence.QuestionMapper;

@Service 
public class  QuestionService
{
  @Autowired
  private QuestionMapper questionMapper;
  
  public Map<Integer, QuestionType> getQuestionTypeMap()
  {
    Map<Integer, QuestionType> map = new HashMap();
    List<QuestionType> typeList = this.questionMapper.getQuestionTypeList();
    for (QuestionType type : typeList) {
      map.put(Integer.valueOf(type.getId()), type);
    }
    return map;
  }
  
  public Question getQuestionByQuestionId(int questionId)
  {
    return this.questionMapper.getQuestionByQuestionId(questionId);
  }
  
  public List<QuestionQueryResult> getQuestionAnalysisListByPointIdAndTypeId(int typeId, int pointId)
  {
    return this.questionMapper.getQuestionAnalysisListByPointIdAndTypeId(typeId, pointId);
  }
  
  public List<Field> getAllField(Page<Field> page)
  {
    return this.questionMapper.getAllField(page);
  }
  
  public Map<Integer, KnowledgePoint> getKnowledgePointByFieldId(Page<KnowledgePoint> page, int... fieldIdList)
  {
    Map<Integer, KnowledgePoint> map = new HashMap();
    if ((fieldIdList != null) && (fieldIdList.length == 0)) {
      fieldIdList = null;
    }
    List<KnowledgePoint> pointList = this.questionMapper.getKnowledgePointByFieldId(fieldIdList, page);
    for (KnowledgePoint point : pointList) {
      map.put(Integer.valueOf(point.getPointId()), point);
    }
    return map;
  }
  
  public Map<Integer, Map<Integer, List<QuestionQueryResult>>> getQuestionMapByFieldId(int fieldId, Page<QuestionQueryResult> page)
  {
    List<QuestionQueryResult> questionList = this.questionMapper.getQuestionListByFieldId(fieldId, page);
    Map<Integer, Map<Integer, List<QuestionQueryResult>>> map = new HashMap();
    for (QuestionQueryResult result : questionList)
    {
      Map<Integer, List<QuestionQueryResult>> tmpMap = (Map)map.get(Integer.valueOf(result.getKnowledgePointId()));
      if (tmpMap == null) {
        tmpMap = new HashMap();
      }
      List<QuestionQueryResult> tmpList = (List)tmpMap.get(Integer.valueOf(result.getQuestionTypeId()));
      if (tmpList == null) {
        tmpList = new ArrayList();
      }
      tmpList.add(result);
      tmpMap.put(Integer.valueOf(result.getQuestionTypeId()), tmpList);
      map.put(Integer.valueOf(result.getKnowledgePointId()), tmpMap);
    }
    return map;
  }
  
  public List<QuestionQueryResult> getQuestionAnalysisListByIdList(List<Integer> idList)
  {
    return this.questionMapper.getQuestionAnalysisListByIdList(idList);
  }
  
  public Map<Integer, QuestionStatistic> getQuestionStaticByFieldId(int fieldId)
  {
    List<QuestionStatistic> statisticList = this.questionMapper.getQuestionStaticByFieldId(fieldId);
    Map<Integer, QuestionStatistic> map = new HashMap();
    for (QuestionStatistic statistic : statisticList) {
      map.put(Integer.valueOf(statistic.getPointId()), statistic);
    }
    return map;
  }
  
  public Map<Integer, Map<Integer, QuestionStatistic>> getTypeQuestionStaticByFieldId(int fieldId)
  {
    List<QuestionStatistic> statisticList = this.questionMapper.getTypeQuestionStaticByFieldId(fieldId);
    Map<Integer, Map<Integer, QuestionStatistic>> map = new HashMap();
    for (QuestionStatistic statistic : statisticList)
    {
      Map<Integer, QuestionStatistic> tmp = (Map)map.get(Integer.valueOf(statistic.getPointId()));
      if (tmp == null) {
        tmp = new HashMap();
      }
      tmp.put(Integer.valueOf(statistic.getQuestionTypeId()), statistic);
      map.put(Integer.valueOf(statistic.getPointId()), tmp);
    }
    return map;
  }
}
